package com.gitee.mars.studyabroad.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.studyabroad.entity.User;
import com.gitee.mars.studyabroad.exception.WorkException;
import com.gitee.mars.studyabroad.mapper.UserMapper;
import com.gitee.mars.studyabroad.request.StudyAbroadApplicationRequest;
import com.gitee.mars.studyabroad.response.StudyAbroadApplicationResponse;
import com.gitee.mars.studyabroad.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.studyabroad.service.IStudyAbroadApplicationService;
import com.gitee.mars.studyabroad.mapper.StudyAbroadApplicationMapper;
import com.gitee.mars.studyabroad.entity.StudyAbroadApplication;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * StudyAbroadApplication 实现层
 *
 * @Author wq
 * @Date 2021-04-22
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class StudyAbroadApplicationServiceImpl extends ServiceImpl<StudyAbroadApplicationMapper, StudyAbroadApplication> implements IStudyAbroadApplicationService {

    @Resource
    private UserMapper userMapper;

    @Override
    public PageInfo<StudyAbroadApplicationResponse> list(StudyAbroadApplicationRequest request) {
        IPage<StudyAbroadApplication> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<StudyAbroadApplication> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(request.getType())) {
            wrapper.eq(StudyAbroadApplication::getType, request.getType());
        }
        if (Objects.nonNull(request.getAcademicDegree())) {
            wrapper.eq(StudyAbroadApplication::getAcademicDegree, request.getAcademicDegree());
        }
        if (StringUtils.isNotEmpty(request.getMajor())) {
            wrapper.like(StudyAbroadApplication::getMajor, request.getMajor());
        }
        if (StringUtils.isNotEmpty(request.getSchool())) {
            wrapper.like(StudyAbroadApplication::getSchool, request.getSchool());
        }
        wrapper.orderByDesc(StudyAbroadApplication::getCreateTime);
        IPage<StudyAbroadApplication> studyAbroadApplicationIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(studyAbroadApplicationIPage.getRecords())) {
            List<StudyAbroadApplicationResponse> list = studyAbroadApplicationIPage.getRecords().stream().map(x -> {
                StudyAbroadApplicationResponse response = new StudyAbroadApplicationResponse();
                BeanUtils.copyProperties(x, response);
                User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, x.getUserId()));
                if (Objects.nonNull(user)) {
                    response.setAvatar(user.getAvatar());
                    response.setNickname(user.getNickname());
                }
                return response;
            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;

    }

    @Override
    public void add(StudyAbroadApplicationRequest request) {
        StudyAbroadApplication application = new StudyAbroadApplication();
        if (StringUtils.isEmpty(request.getSchool())) {
            throw new WorkException("请输入学校名称");
        }
        if (StringUtils.isEmpty(request.getMajor())) {
            throw new WorkException("请输入专业名称");
        }
        application.setApplyTime(request.getApplyTime());
        application.setAcquireTime(request.getAcquireTime());
        application.setMajor(request.getMajor());
        application.setSchool(request.getSchool());
        application.setCreateTime(new Date());
        application.setUpdateTime(new Date());
        application.setType(request.getType());
        application.setUserId(request.getUserId());
        application.setGpaScore(request.getGpaScore());
        application.setBackGround(request.getBackGround());
        application.setAcademicDegree(request.getAcademicDegree());
        application.setIeltsScore(request.getIeltsScore());
        application.setToeflScore(request.getToeflScore());
        save(application);
    }
}